﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary
{
    /// <summary>
    /// PipeLine.xaml 的交互逻辑
    /// </summary>
    public partial class PipeLine : UserControl
    {
        public PipeLine()
        {
            InitializeComponent();
        }

        private LinearGradientBrush HorizontalBackground => (LinearGradientBrush)Resources["HorizontalBackground"];//这是一个只读属性
        private LinearGradientBrush VerticalBackground => (LinearGradientBrush)Resources["VerticalBackground"];//这是一个只读属性

        public Orientation? Direction
        {
            get { return (Orientation)GetValue(DirectionProperty); }
            set { SetValue(DirectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Direction.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DirectionProperty =
            DependencyProperty.Register("Direction", typeof(Orientation?), typeof(PipeLine), new PropertyMetadata(null, new PropertyChangedCallback(OnDirectionChanged)));

        private static void OnDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PipeLine pipeLine = d as PipeLine;
            if ((Orientation?)e.NewValue == Orientation.Horizontal)
            {
                pipeLine.liquidline.X1 = 0;
                pipeLine.liquidline.Y1 = 0;
                pipeLine.liquidline.X2 = pipeLine.Width;
                pipeLine.liquidline.Y2 = 0;
                pipeLine.liquidline.StrokeThickness = pipeLine.Height;
                pipeLine.border.Background = pipeLine.HorizontalBackground;
            }
            else
            {
                pipeLine.liquidline.X1 = 0;
                pipeLine.liquidline.Y1 = 0;
                pipeLine.liquidline.X2 = 0;
                pipeLine.liquidline.Y2 = pipeLine.Height;
                pipeLine.liquidline.StrokeThickness = pipeLine.Width;
                pipeLine.border.Background = pipeLine.VerticalBackground;
            }

        }

        public Brush LiquidColor
        {
            get { return (Brush)GetValue(LiquidColorProperty); }
            set { SetValue(LiquidColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LiquidColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LiquidColorProperty =
            DependencyProperty.Register("LiquidColor", typeof(Brush), typeof(PipeLine), new PropertyMetadata(Brushes.Brown));




        public int? DirectionFlow
        {
            get { return (int?)GetValue(DirectionFlowProperty); }
            set { SetValue(DirectionFlowProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DirectionFlow.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DirectionFlowProperty =
            DependencyProperty.Register("DirectionFlow", typeof(int?), typeof(PipeLine), new PropertyMetadata(null,new PropertyChangedCallback(DirectionFlowOnChanged)));

        /// <summary>
        /// 当DirectionFlow属性发生变化时，需要会调用方法【切换状态】
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void DirectionFlowOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if((int?)e.NewValue == 0)
            {
                VisualStateManager.GoToState(d as PipeLine, "Stop", false);
            }
            if ((int?)e.NewValue == 1)
            {
                VisualStateManager.GoToState(d as PipeLine, "WEFlow", false);
            }
            if ((int?)e.NewValue == -1)
            {
                VisualStateManager.GoToState(d as PipeLine, "EEFlow", false);
            }

        }
    }
}
